package com.lambda.hybrid.handler;

import com.lambda.hybrid.Callback;
import com.lambda.hybrid.utils.ACache;

import org.json.JSONObject;

public class GetCacheHandler extends AbstractHandler {
    private ACache mCache;

    public GetCacheHandler(ACache cach) {
        mCache = cach;
    }

    @Override
    public void onHandle(String data, Callback callback) throws Exception {
        JSONObject table = new JSONObject(data);
        final String key = table.getString("key");
        JSONObject resp = new JSONObject();
        String value = mCache.getAsString(key);
        resp.put("value", value == null ? JSONObject.NULL : value);
        callback.invoke(resp.toString());
    }
}
